跳到主要内容

二维矩阵(Mat2D)

表示二维变换矩阵(2D transformation matrix),包含缩放、旋转、错切与位移分量。

-- Construct a matrix manually
local m = Mat2D.identity()

m.xx = 2 -- scale x
m.yy = 3 -- scale y
m.tx = 50 -- translate x
m.ty = 100 -- translate y

local r = m * Vector.xy(10, 20)

-- (10,20) → (20,60) → (70,160)
print(r.x, r.y) -- 70, 160

字段(Fields)

xx

矩阵 xx 分量。

local m = Mat2D.identity()

-- Double horizontal scale
m.xx = 2

local r = m * Vector.xy(10, 5)
print(r.x, r.y) -- 20, 5

xy

矩阵 xy 分量。

local m = Mat2D.identity()

-- Add horizontal skew
m.xy = 1

local r = m * Vector.xy(10, 5)
-- x = 10 + (1 * 5)
print(r.x, r.y) -- 15, 5

yx

矩阵 yx 分量。

local m = Mat2D.identity()

-- Add vertical skew
m.yx = 1

local r = m * Vector.xy(10, 5)
-- y = 5 + (1 * 10)
print(r.x, r.y) -- 10, 15

yy

矩阵 yy 分量。

local m = Mat2D.identity()

-- Triple vertical scale
m.yy = 3

local r = m * Vector.xy(4, 5)
print(r.x, r.y) -- 4, 15

tx

x 轴平移量。

local m = Mat2D.identity()

m.tx = 50

local r = m * Vector.xy(10, 20)
print(r.x, r.y) -- 60, 20

ty

y 轴平移量。

local m = Mat2D.identity()

m.ty = 100

local r = m * Vector.xy(10, 20)
print(r.x, r.y) -- 10, 120

withTranslation

根据 x,yVector 创建平移矩阵。

-- From numbers
local t1 = Mat2D.withTranslation(50, 100)

-- From a Vector
local pos = Vector.xy(50, 100)
local t2 = Mat2D.withTranslation(pos)

withScale

根据 x,yVector 创建缩放矩阵。

-- Uniform scale (y defaults to x if omitted)
local s1 = Mat2D.withScale(2)

-- Non-uniform scale
local s2 = Mat2D.withScale(2, 3)

-- From a Vector
local scale = Vector.xy(2, 3)
local s3 = Mat2D.withScale(scale)

withScaleAndTranslation

根据数值或向量创建“缩放 + 平移”矩阵。

-- Numbers Overloaded

-- Scale by (2, 3) then translate by (50, 100)
local st = Mat2D.withScaleAndTranslation(2, 3, 50, 100)

local p = Vector.xy(10, 20)
local r = st * p

-- (10,20) -> scaled (20,60) -> translated (70,160)
print(r.x, r.y) -- 70, 160

-- Vectors Overloaded

local st = Mat2D.withScaleAndTranslation(
Vector.xy(2, 3), -- scale
Vector.xy(50, 100) -- position / translation
)

local r = st * Vector.xy(10, 20)
print(r.x, r.y) -- 70, 160

构造器(Constructors)

values

使用给定分量创建矩阵。

-- xx=1, xy=skewX (horizontal skew), yx=skewY (vertical skew), yy=1, tx=0, ty=0Ô
local newMat = Mat2D.values(1, self.skewY, self.skewX, 1, 0, 0)Ô

identity

返回单位矩阵(identity matrix)。

-- xx = 1, xy = 0, yx = 0, yy = 1, tx = 0, ty = 0
local newMat = Mat2D.identity()

withRotation

根据弧度角创建旋转矩阵。

local rot = Mat2D.withRotation(math.rad(90))
local r = rot * Vector.xy(1, 0)

print(math.floor(r.x + 0.5), math.floor(r.y + 0.5)) -- 0, 1

静态函数(Static Functions)

invert

input 的逆矩阵写入 output。若可逆返回 true

local m = Mat2D.withTranslation(50, 100)

local inv = Mat2D.identity()
local ok = Mat2D.invert(inv, m)
print(ok) -- true

local p = Vector.xy(10, 20)
local r = inv * (m * p)

-- Applying m then inv returns the original point
print(r.x, r.y) -- 10, 20

方法(Methods)

invert

返回矩阵逆;不可逆时返回 nil

local m = Mat2D.withTranslation(50, 100)
local inv = m:invert()

if inv then
local p = Vector.xy(10, 20)
local result = inv * (m * p)
print(result.x, result.y) -- 10, 20
end

isIdentity

若为单位变换返回 true

local newMat = Mat2D.identity()
print(newMat:isIdentity())

__eq

当两个矩阵全部分量相等时返回 true

local a = Mat2D.values(1, 0, 0, 1, 0, 0)
local b = Mat2D.identity()

print(a == b) -- true

__mul

将矩阵作用于 Vector 并返回结果。

local translation = Mat2D.values(1, 0, 0, 1, 50, 100)
local point = Vector.xy(10, 20)

-- Multiply matrix by vector
local result = translation * point

print(result.x, result.y) -- 60, 120

__mul

返回当前矩阵与另一个矩阵的乘积。

local translation = Mat2D.values(1, 0, 0, 1, 50, 100)
local scale = Mat2D.values(2, 0, 0, 2, 0, 0)

-- Combine transforms
local combined = translation * scale

local point = Vector.xy(10, 20)
local result = combined * point

print(result.x, result.y) -- 70, 140